#!/bin/bash
#
# Xiange Linux build scripts

# Short one-line description of this package.
DESCRIPTION="php running in fast cgi mode"

# Homepage, not used by Portage directly but handy for developer reference
HOMEPAGE="http://php.net/"

# Point to any required sources; these will be automatically downloaded by
# gpkg. 
# $N = package name, such as autoconf, x-org
# $V = package version, such as 2.6.10

#SRC_URI="http://foo.bar.com/$N-$V.tar.bz2"
SRC_URI="http://mirrors.dev.shopex.cn/lnmpp/sources/php-5.2.14.tar.gz"


# Binary package URI.
BIN_URI=""


# Runtime Depend
RDEPEND=""

# Build time depend
DEPEND="${RDEPEND}"



#init 
xgb_init()
{
    echo "init $N-$V$R build script..."
    #add web server running user
    id www > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        useradd -s /sbin/nologin -M  www
    fi
}

#unpack
xgb_unpack()
{
    #unpard file from $XGPATH_SOURCE to current directory.
    echo "Unpacking to `pwd`"
    tar xf $XGPATH_SOURCE/`basename $SRC_URI`
}

#config
xgb_config()
{
    echo "config $N-$V$R..."
    #fist, cd build directory
    gzip -cd $XGPATH_SOURCE/php-5.2.14-fpm-0.5.14.diff.gz | patch -d $N-$V$R -p1
    cd $N-$V$R
    
    #second, add package specified config params to XGB_CONFIG
    XGB_CONFIG+=" --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-mysql=/usr/local/mysql \
    --with-mysqli=/usr/local/mysql/bin/mysql_config \
    --with-iconv-dir \
    --with-freetype-dir \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib \
    --with-libxml-dir=/usr --enable-xml --disable-rpath \
    --enable-discard-path \
    --enable-magic-quotes \
    --enable-safe-mode \
    --enable-bcmath \
    --enable-shmop \
    --enable-sysvsem \
    --enable-inline-optimization \
    --with-curl \
    --with-curlwrappers \
    --enable-mbregex \
    --enable-fastcgi \
    --enable-fpm \
    --enable-force-cgi-redirect \
    --enable-mbstring \
    --with-mcrypt \
    --enable-ftp \
    --with-gd \
    --enable-gd-native-ttf \
    --with-openssl \
    --with-mhash \
    --enable-pcntl \
    --enable-sockets \
    --with-xmlrpc \
    --enable-zip \
    --enable-soap \
    --without-pear \
    --with-gettext \
    --with-mime-magic  "
        
    #Third, call configure with $XGB_CONFIG
    ./configure $XGB_CONFIG
}

#build
xgb_build()
{
    echo "make $N-$V$R..."
    make ZEND_EXTRA_LIBS='-liconv'
}

#check
xgb_check()
{
    echo "checking $N-$V$R.."
    #make check
}

#install
xgb_install()
{
    echo "install to $XGPATH_DEST..."    
    #install everything to $XGPATH_DEST
    make INSTALL_ROOT="$XGPATH_DEST" install
    cp php.ini-dist $XGPATH_DEST/usr/local/php/etc/php.ini
    mkdir -pv $XGPATH_DEST/etc/init.d
    install -v -m755 $XGPATH_DEST/usr/local/php/sbin/php-fpm $XGPATH_DEST/etc/init.d/php-fpm    
}

#post install
xgb_postinst()
{
    echo "running after package installed..."
    #adjust php.ini
    sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"#'  /usr/local/php/etc/php.ini
    sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/php/etc/php.ini
    sed -i 's/post_max_size = 8M/post_max_size = 64M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 64M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=0/cgi.fix_pathinfo=1/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    #adjust php-fpm
    sed -i 's,<!--.*<value name=\"user\">nobody</value>.*-->,<value name=\"user\">www</value>,g'  /usr/local/php/etc/php-fpm.conf
    sed -i 's,<!--.*<value name=\"group\">nobody</value>.*-->,<value name=\"group">www</value>,g' /usr/local/php/etc/php-fpm.conf
    sed -i 's,<value name=\"listen_address\">127.0.0.1:9000</value>,<value name=\"listen_address\">/tmp/php_fcgi.sock</value>,g' /usr/local/php/etc/php-fpm.conf
    sed -i 's,<value name=\"max_children\">5</value>,<value name=\"max_children\">128</value>,g' /usr/local/php/etc/php-fpm.conf
    #self start 
    echo '# Comments to support chkconfig on RedHat Linux' >> /etc/init.d/php-fpm 
    echo '# chkconfig: 2345 65 37' >> /etc/init.d/php-fpm 
    echo '# description: A php fast cgi interface' >> /etc/init.d/php-fpm      
    chkconfig --add php-fpm
    echo "<?php phpinfo();" > /data/httpd/info.php
    service php-fpm start
}

#pre remove
xgb_prerm()
{
    echo "running before package delete..."
}

#post remove
xgb_postrm()
{
    echo "running after package delete..."
}
